Page({
  data: {
    curring: -1,
    detail: [
      {
        id: '1', title: '艾绒的主要原料是', answer: '3', array: [
          { name: '根', usname: false }, { name: '茎', usname: false },
          { name: '叶', usname: false }, { name: '花', usname: false },
        ]
      },
      {
        id: '2', title: '艾草属什么之性的植物', answer: '1', array: [
          { name: '纯阳之性', usname: false }, { name: '纯阴之性', usname: false },
          { name: '阴阳互补', usname: false }, { name: '阴阳互斥', usname: false },
        ]
      },
      {
        id: '3', title: '艾叶的归途是', answer: '2', array: [
          { name: '化痰药', usname: false }, { name: '开窍药', usname: false },
          { name: '补阳药', usname: false }, { name: '利水通淋药', usname: false },
        ]
      },
      {
        id: '4', title: '以下哪一个不属于艾草的功效与作用', answer: '1', array: [
          { name: '滋润肌肤', usname: false }, { name: '祛风除湿', usname: false },
          { name: '暖宫散寒', usname: false }, { name: '温通经络', usname: false },
        ]
      }
    ],
    number: 0,
    answer: 0,
  },
  previous: function (e) {
    this.setData({
      number: this.data.number - 1,
      curring: this.data.curring - 1,
    })
  },
  radioChange: function (e) {
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    let detail = this.data.detail
    for (let i = 0; i < detail.length; i++) {
      if (detail[i].id == id) {
        detail[i].array[index].usname = true
        for (let j = 0; j < detail[i].array.length; j++) {
          if (j != index) {
            detail[i].array[j].usname = false
          }
        }
      }
    }
    this.setData({
      detail: detail
    })
  },
  nextstep: function (e) {
    let detail = this.data.detail
    let number = this.data.number
    let curring = this.data.curring
    let usname = 0;
    for (let i = 0; i < detail[number].array.length; i++) {
      if (!detail[number].array[i].usname) {
        usname++
      }
    }
    if (usname == detail[number].array.length) {
      wx.showToast({
        title: '答题选项不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    curring++
    number++
    if (curring > 3) {
      curring = -1
    }
    this.setData({
      curring: curring,
      number: number,
    })
  },
  subsic: function (e) {
    let detail = this.data.detail
    let answer = 0
    let letter = ''
    for (let i = 0; i < detail.length; i++) {
      for (let j = 0; j < detail[i].array.length; j++) {
        if (detail[i].array[j].usname) {
          letter = detail[i].answer - 1
          if (letter == j) {
            answer++
          }
        }
      }
    }
    wx.showToast({
      title: '答对了:' + answer + '题,奖励' + answer * 10 + 'g雨滴',
      icon: 'none',
      duration: 2000
    })

    // 将得到的雨滴奖励返回到上一个页面中去
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    //直接调用上一个页面的 setData() 方法，把数据存到上一个页面中去
    prevPage.setData({
      water: prevPage.data.water + answer * 10
    })



    // 延时两秒后返回到种树页面
    setTimeout(() => {
      wx.navigateBack({
        delta: 1,
      })
    }, 2000)

  },
})
